//
//  LJJGoodsDetailViewController.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/18.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJGoodsDetailViewController.h"
#import "LJJGoodsTopImageView.h"
#import "LJJDetailImageModel.h"
#import "LJJGoodsDetailTitleLabelView.h"
#import "LJJDetailTitleViewModel.h"
#import "LJJGoodsDetailContentView.h"
#import "LJJDetailContentModel.h"
#import "LJJGoodsBottomImageView.h"
#import "LJJGoodsThreeButtonView.h"
#import "LJJLoginViewController.h"

@interface LJJGoodsDetailViewController ()

@property (strong, nonatomic) UIScrollView *scrollView;    /** 底部的scrollview **/
@property (strong, nonatomic) LJJGoodsTopImageView *topImageView;    /** 头部的轮播图 **/
@property (strong, nonatomic) LJJGoodsDetailTitleLabelView *titleLabelView;    /** 商品标题 **/
@property (strong, nonatomic) LJJGoodsDetailContentView *contentView;    /** 商品详情 **/
@property (strong, nonatomic) LJJGoodsBottomImageView *bottomImageView;    /** 底部图片 **/
@property (assign, nonatomic) CGFloat scrollContentHeight;    /** scrollView的滚动范围 **/
@property (strong, nonatomic) LJJGoodsThreeButtonView *threeButtonView;    /** 底部的三个按钮的view **/

@end

@implementation LJJGoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollContentHeight = 365.0;
    [self configUI];
    [self getImageData];
    [self getTitleViewData];
    [self getContentViewData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)setScrollContentHeight:(CGFloat)scrollContentHeight
{
    _scrollContentHeight = scrollContentHeight;
    _scrollView.contentSize = CGSizeMake(0, scrollContentHeight);
}
#pragma mark -
#pragma mark - method
- (void)configUI
{
    [self.view addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.topImageView];
    [self.scrollView addSubview:self.titleLabelView];
    [self.scrollView addSubview:self.contentView];
    [self.scrollView addSubview:self.bottomImageView];
    [self.view addSubview:self.threeButtonView];
    __weak typeof (self) weakSelf = self;
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view).with.insets(UIEdgeInsetsMake(0, 0, 45, 0));
    }];
    [self.titleLabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.topImageView.mas_bottom);
        make.right.left.equalTo(weakSelf.view);
    }];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLabelView.mas_bottom);
        make.left.right.equalTo(weakSelf.view);
    }];
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.contentView.mas_bottom);
    }];
}
- (void)getImageData
{
    [self getData:@"appGoods/findGoodsImgList.do" param:@{@"GoodsId":self.detailGoodsID} success:^(id responseObject) {
        NSArray *imageArray = [LJJDetailImageModel mj_objectArrayWithKeyValuesArray:responseObject];
        NSMutableArray *topImageArray = [NSMutableArray array];
        for (LJJDetailImageModel *imageModel in imageArray) {
            if ([imageModel.ImgType isEqualToString:@"1"]) {
                [topImageArray addObject:imageModel.ImgView];
            }
        }
        _topImageView.imageArray = topImageArray;
        _bottomImageView.imageArray = imageArray;
    } error:^(NSError *error) {
        LJJLog(@"getImageDataError: --- %@", error);
    }];
}
- (void)getTitleViewData
{
    [self getData:@"appGoods/findGoodsDetail.do" param:@{@"GoodsId":self.detailGoodsID} success:^(id responseObject) {
        LJJDetailTitleViewModel *titleModel = [LJJDetailTitleViewModel mj_objectWithKeyValues:responseObject];
        self.titleLabelView.model = titleModel;
        self.topImageView.buyNum = titleModel.BuyCount;
        self.titleLabelView.countryImage = self.countryImage;
    } error:^(NSError *error) {
        LJJLog(@"getTitleViewDataError: --- %@", error);
    }];
}
- (void)getContentViewData
{
    [self getData:@"appGoods/findGoodsDetailList.do?" param:@{@"GoodsId":self.detailGoodsID} success:^(id responseObject) {
        NSArray *modelArray = [LJJDetailContentModel mj_objectArrayWithKeyValuesArray:responseObject];
        self.contentView.contentArray = modelArray;
    } error:^(NSError *error) {
        LJJLog(@"getContentViewDataError: --- %@", error);
    }];
}
- (void)addToBuyCar
{
    NSDictionary *landingDic = [[NSUserDefaults standardUserDefaults]valueForKey:@"ISLOGIN"];
    if (landingDic.count > 0) {
        [self getData:@"appShopCart/insert.do" param:@{@"MemberId":landingDic[@"MemberId"],@"GoodsId":self.detailGoodsID} success:^(id responseObject) {
            if ([responseObject[@"result"] isEqualToString:@"success"]) {
                [self showTostMessage:@"商品已加入购物车"];
            }else{
                [self showTostMessage:@"加入购物车失败"];
            }
        } error:^(NSError *error) {
            LJJLog(@"addToBuyCarError: --- %@", error);
        }];
    }else{
        //跳转到登录页面
        LJJLoginViewController *loginVC = [[LJJLoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}
#pragma mark -
#pragma mark - lazy method
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.contentSize = CGSizeMake(0, 10000);
    }
    return _scrollView;
}
- (LJJGoodsTopImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView = [[LJJGoodsTopImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 380)];
    }
    return _topImageView;
}
- (LJJGoodsDetailTitleLabelView *)titleLabelView
{
    if (!_titleLabelView) {
        _titleLabelView = [[LJJGoodsDetailTitleLabelView alloc] init];
        _titleLabelView.backgroundColor = [UIColor whiteColor];
        __weak typeof(self) weakSelf = self;
        _titleLabelView.heightBlock = ^(CGFloat height){
            [weakSelf.titleLabelView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            weakSelf.scrollContentHeight += height;
        };
    }
    return _titleLabelView;
}
- (LJJGoodsDetailContentView *)contentView
{
    if (!_contentView) {
        _contentView = [[LJJGoodsDetailContentView alloc] init];
        __weak typeof(self) weakSelf = self;
        _contentView.heightBlock = ^(CGFloat height){
          [weakSelf.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
              make.height.mas_equalTo(height);
          }];
            weakSelf.scrollContentHeight += height;
        };
    }
    return _contentView;
}
- (LJJGoodsBottomImageView *)bottomImageView
{
    if (!_bottomImageView) {
        _bottomImageView = [[LJJGoodsBottomImageView alloc] init];
        __weak typeof(self) weakSelf = self;
        _bottomImageView.heightBlock = ^(CGFloat height){
            [weakSelf.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            weakSelf.scrollContentHeight += height;
        };
    }
    return _bottomImageView;
}
- (LJJGoodsThreeButtonView *)threeButtonView
{
    if (!_threeButtonView) {
        _threeButtonView = [[LJJGoodsThreeButtonView alloc] initWithFrame:CGRectMake(0, HEIGHT - 45 - 64, WIDTH, 45)];
        __weak typeof(self) weakSelf = self;
        _threeButtonView.block = ^(){
            [weakSelf addToBuyCar];
        };
    }
    return _threeButtonView;
}

@end
